/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package eypweb_bsc;

import java.io.IOException;
import javafx.event.EventType;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Kohalmi Istvan
 */

// felsoszintu vezerlo osztaly
public class Director {

    // osztalyszintu referencia az egyetlen peldanyra
    private static Director directorInstance;
    // stiluslap neve
    private String styleName;
    // foablak referencia
    private Stage primaryStage;
    // bejelentkezesi kepernyo
    private Scene loginScreenScene;
    // fomenu
    private Scene mainMenuScene;
    // muveletrogzito kepernyo
    private Scene operationScene;
    // eszkozcsoport rogzito kepernyo
    private Scene instrumentScene;
    // portfolio rogzito kepernyo
    private Scene portfolioScene;
    // befektetescsoport rogzito kepernyo
    private Scene investmentGroupScene;
    // deviza rogzito kepernyo
    private Scene currencyScene;
    // tranzakciotipus rogzito kepernyo
    private Scene transactionScene;
    // vagyonkezelo rogzito kepernyo
    private Scene trusteeScene;
    // ertekpapir rogzito kepernyo
    private Scene securitiesScene;
    // piaci ertekeles kepernyo
    private Scene evaluationScene;
    // adatelemzes kepernyo
    private Scene queryScene;
    // aktiv vagyonkezelo kivalasztasa
    private Stage trusteeStage;
    // csatlakozas informaciok
    private AccessInfo accessInfo;

    // private lathatosagu konstruktor
    private Director() {
    }

    // visszaadja az egyeduli oeldany referenciajat
    // ha meg nincs peldanyosit
    public static synchronized Director getInstance() {
        if (directorInstance == null) {
            directorInstance = new Director();
        }
        return directorInstance;
    }

    public void setStage(Stage stage) {
        primaryStage = stage;
    }

    public Stage getStage() {
        return primaryStage;
    }

    public AccessInfo getAccessInfo() {
        return accessInfo;
    }

    // elmenti a csatlakozas informaciokat
    public void saveAccess(String ipAddress, int port, String dbName, String u, String p) {
        accessInfo = new AccessInfo(ipAddress, port, dbName, u, p);
    }

    // bejelentkezesi kepernyo betoltese
    public void initializeLoginScreen() {
        styleName = "egyes";
        try {
            if (loginScreenScene == null) {
                // szinter graf betoltese
                Parent loginScreen = FXMLLoader.load(getClass().getResource("LoginScreen.fxml"));
                // vaszon letrehozasa
                loginScreenScene = new Scene(loginScreen);
                // stiluslap hozzarendelese
                loginScreenScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                // CSS osztaly kotes
                loginScreenScene.getRoot().getStyleClass().add("scene");
            }
            // vaszon betoltese
            primaryStage.setScene(loginScreenScene);
            // ablak cim megadasa
            primaryStage.setTitle("Értékpapír nyilvántartó rendszer");
            // ablak mutatasa
            primaryStage.show();
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }

    // fomenu betoltese
    public void initializeMainMenu() {
        try {
            if (mainMenuScene == null) {
                Parent mainMenu = FXMLLoader.load(getClass().getResource("MainMenu.fxml"));
                mainMenuScene = new Scene(mainMenu);
                mainMenuScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                mainMenuScene.getRoot().getStyleClass().add("scene");
            }
            // ablak meretenek allitasa
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            // ablak elhelyezese a monitoron
            primaryStage.setX(300);
            primaryStage.setY(100);
            // kepernyo valtas
            primaryStage.setScene(mainMenuScene);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }

    // muveletrogzito kepernyo betoltese
    public void initializeOperationScreen() {
        try {
            if (operationScene == null) {
                Parent transactions = FXMLLoader.load(getClass().getResource("OperationRegistry.fxml"));
                operationScene = new Scene(transactions);
                operationScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                operationScene.getRoot().getStyleClass().add("scene");
            }
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            primaryStage.setScene(operationScene);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }

    // eszkozcsoport rogzito kepernyo betoltese
    public void initializeInstrumentScreen() {
        try {
            if (instrumentScene == null) {
                Parent instrument = FXMLLoader.load(getClass().getResource("InstrumentGroupRegistry.fxml"));
                instrumentScene = new Scene(instrument);
                instrumentScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                instrumentScene.getRoot().getStyleClass().add("scene");
            }
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            primaryStage.setScene(instrumentScene);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }

    // portfolio rogzito kepernyo betoltese
    public void initializePortfolioScreen() {
        try {
            if (portfolioScene == null) {
                Parent portfolio = FXMLLoader.load(getClass().getResource("PortfolioRegistry.fxml"));
                portfolioScene = new Scene(portfolio);
                portfolioScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                portfolioScene.getRoot().getStyleClass().add("scene");
            }
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            primaryStage.setScene(portfolioScene);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }

    // befektetescsoport rogzito kepernyo betoltese
    public void initializeInvestmentGroupScreen() {
        try {
            if (investmentGroupScene == null) {
                Parent investmentGroup = FXMLLoader.load(getClass().getResource("InvestmentGroupRegistry.fxml"));
                investmentGroupScene = new Scene(investmentGroup);
                investmentGroupScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                investmentGroupScene.getRoot().getStyleClass().add("scene");
            }
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            primaryStage.setScene(investmentGroupScene);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }

    // deviza rogzito kepernyo betoltese
    public void initializeCurrencyScreen() {
        try {
            if (currencyScene == null) {
                Parent currency = FXMLLoader.load(getClass().getResource("CurrencyRegistry.fxml"));
                currencyScene = new Scene(currency);
                currencyScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                currencyScene.getRoot().getStyleClass().add("scene");
            }
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            primaryStage.setScene(currencyScene);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }

    // tranzakciotipus rogzito kepernyo betoltese
    public void initializeTransactionScreen() {
        try {
            if (transactionScene == null) {
                Parent transaction = FXMLLoader.load(getClass().getResource("TransactionRegistry.fxml"));
                transactionScene = new Scene(transaction);
                transactionScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                transactionScene.getRoot().getStyleClass().add("scene");
            }
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            primaryStage.setScene(transactionScene);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }

    // vagyonkezelo rogzito kepernyo betoltese
    public void initializeTrusteeScreen() {
        try {
            if (trusteeScene == null) {
                Parent trustee = FXMLLoader.load(getClass().getResource("TrusteeRegistry.fxml"));
                trusteeScene = new Scene(trustee);
                trusteeScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                trusteeScene.getRoot().getStyleClass().add("scene");
            }
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            primaryStage.setScene(trusteeScene);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }

    // ertekpapir rogzito kepernyo betoltese
    public void initializeSecuritiesScreen() {
        try {
            if (securitiesScene == null) {
                Parent securities = FXMLLoader.load(getClass().getResource("SecuritiesRegistry.fxml"));
                securitiesScene = new Scene(securities);
                securitiesScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                securitiesScene.getRoot().getStyleClass().add("scene");
            }
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            primaryStage.setScene(securitiesScene);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }
    
    // piaci ertekeles kepernyo betoltese
    // a felulet kesz de logika meg nincs mogotte
    public void initializeEvaluationScreen() {
        try {
            if (evaluationScene == null) {
                Parent evaluation = FXMLLoader.load(getClass().getResource("EvaluationRegistry.fxml"));
                evaluationScene = new Scene(evaluation);
                evaluationScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                evaluationScene.getRoot().getStyleClass().add("scene");
            }
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            primaryStage.setScene(evaluationScene);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }

    // vagyonkezelo kivalasztasa kepernyo betoltese
    public void initializeTrusteeSelectionScreen()
    {
        if (trusteeStage==null)
        {
            trusteeStage = new Stage();
            trusteeStage.setWidth(500);
            trusteeStage.setHeight(200);
            try{
                Parent trustee = FXMLLoader.load(getClass().getResource("TrusteeScreen.fxml"));
                Scene trScene = new Scene(trustee);
                trScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                trScene.getRoot().getStyleClass().add("scene");
                trusteeStage.setScene(trScene);
                trusteeStage.setTitle("Vagyonkezelő kiválasztása");
                trusteeStage.show();
            }
            catch(IOException ioe)
            {
                System.out.println(ioe.getMessage());
                System.out.println(ioe.getStackTrace());
            }
        }
        else
        {
            trusteeStage.show();
        }
    }
    
    // adatelemzes kepernyo betoltese
    public void initializeQueryScene()
    {
        try {
            if (queryScene == null) {
                Parent query = FXMLLoader.load(getClass().getResource("QueryScreen.fxml"));
                queryScene = new Scene(query);
                queryScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
                queryScene.getRoot().getStyleClass().add("scene");
            }
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            // a kepernyo meg fejlesztes alatt all
            // primaryStage.setScene(queryScene);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            System.out.println(ioe.getStackTrace());
        }
    }
    
    // vagyonkezelo kivalasztasa
    public void selectTrustee(Trustee t)
    {
        accessInfo.setTrusteeId(t.getId());
        accessInfo.setTrusteeSign(t.getIdTrustee());
        accessInfo.setTrusteeName(t.getShortName());
    }
    
    // stilus kivalasztasa
    // funkcio fejlesztes alatt
    public void reskin(String s) {
        styleName = s;
        if (loginScreenScene != null) {
            loginScreenScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
        if (mainMenuScene != null) {
            mainMenuScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
        if (operationScene != null) {
            operationScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
        if (instrumentScene != null) {
            instrumentScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
        if (portfolioScene != null) {
            portfolioScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
        if (investmentGroupScene != null) {
            investmentGroupScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
        if (currencyScene != null) {
            currencyScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
        if (transactionScene != null) {
            transactionScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
        if (trusteeScene != null) {
            trusteeScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
        if (securitiesScene != null) {
            securitiesScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
        if (evaluationScene != null) {
            evaluationScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
        if (queryScene != null) {
            queryScene.getStylesheets().add(getClass().getResource(styleName + ".css").toExternalForm());
        }
    }
}
